import axios from 'axios';

/**
 * 获取账户信息
 */
export const getAccountInfo = () => {
  return axios.get('/api/account/info', {
    headers: {
      'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  }).then(res => res.data);
};

/**
 * 获取充值记录
 * @param {Object} params 查询参数
 * @param {string} params.phone 用户手机号，用于筛选指定用户的充值记录
 * @param {string} params.recharge_type 充值类型(VIP/EMAIL/CUSTOM)，可选
 * @param {string} params.payment_method 支付方式(WECHAT/ALIPAY)，可选
 * @param {number} params.status 状态(0-成功,1-失败)，可选
 * @param {number} params.limit 获取记录的最大数量
 * @returns {Promise} 返回包含充值记录的Promise
 *   成功响应格式可能为:
 *   1. {code: 200, data: Array<Record>} - 直接返回记录数组
 *   2. {code: 200, data: {records: Array<Record>, totalAmount: number}} - 返回带有总额的对象
 *   其中Record对象可能使用驼峰命名法或下划线命名法
 */
export const getTransactions = (params) => {
  return axios.get('/api/account/recharge-records', {
    params,
    headers: {
      'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  }).then(res => {
    // console.log('API返回的充值记录原始数据:', res.data);
    return res.data;
  }).catch(error => {
    // console.error('获取充值记录API错误:', error);
    throw error;
  });
};

/**
 * 账户充值
 * @param {Object} data 包含充值金额和描述
 * @param {number} data.amount 充值金额
 * @param {string} data.description 充值说明
 * @param {string} data.payMethod 支付方式
 * @param {string} data.phone 用户手机号（唯一标识）
 */
export const recharge = (data) => {
  return axios.post('/api/account/recharge', data, {
    headers: {
      'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  }).then(res => res.data);
};

// 购买导出配额
export const buyExportQuota = (amount) => {
  return axios.post('/api/account/buy-export-quota', { amount }, {
    headers: {
      'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  }).then(res => res.data);
};

// 购买搜索配额
export const buySearchQuota = (amount) => {
  return axios.post('/api/account/buy-search-quota', { amount }, {
    headers: {
      'Authorization': `Bearer ${localStorage.getItem('token')}`
    }
  }).then(res => res.data);
}; 